package cn.edu.scnu.semart.goods.controller;

import cn.edu.scnu.semart.goods.service.FavoriteService;
import cn.edu.scnu.semart.model.goods.Favorite;
import cn.edu.scnu.semart.model.goods.Goods;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户收藏Controller
 */
@RestController
@RequestMapping("/favorite")
//@CrossOrigin
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 添加收藏
     */
    @PostMapping("/add")
    public Map<String, Object> addFavorite(
            @RequestParam Integer userId,
            @RequestParam Integer goodsId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = favoriteService.addFavorite(userId, goodsId);
            result.put("success", success);
            if (success) {
                result.put("message", "收藏成功");
            } else {
                result.put("message", "收藏失败，可能已经收藏过了");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 取消收藏
     */
    @PostMapping("/remove")
    public Map<String, Object> removeFavorite(
            @RequestParam Integer userId,
            @RequestParam Integer goodsId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = favoriteService.removeFavorite(userId, goodsId);
            result.put("success", success);
            if (success) {
                result.put("message", "取消收藏成功");
            } else {
                result.put("message", "取消收藏失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    public Map<String, Object> checkFavorite(
            @RequestParam Integer userId,
            @RequestParam Integer goodsId) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean isFavorite = favoriteService.isFavorite(userId, goodsId);
            result.put("success", true);
            result.put("data", isFavorite);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取用户收藏的商品列表（包含商品详情）
     */
    @GetMapping("/user/{userId}")
    public Map<String, Object> getUserFavorites(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> result = new HashMap<>();
        try {
            Page<Goods> page = new Page<>(current, size);
            IPage<Goods> goodsPage = favoriteService.getUserFavorites(userId, page);

            result.put("success", true);
            result.put("data", goodsPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取用户收藏数量
     */
    @GetMapping("/count/{userId}")
    public Map<String, Object> getUserFavoriteCount(@PathVariable Integer userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer count = favoriteService.getUserFavoriteCount(userId);
            result.put("success", true);
            result.put("data", count);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
} 